package com.yearEndWorkload.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.common.YearEndExcelName;
import com.yearEndWorkload.pojo.entity.QueryCall;
import com.yearEndWorkload.pojo.entity.WorkloadLoad;
import com.yearEndWorkload.pojo.form.add.AddWorkloadLoadForm;
import com.yearEndWorkload.pojo.form.update.UpdateWorkloadLoadForm;
import com.yearEndWorkload.pojo.query.TeacherWorkloadQuery;
import com.yearEndWorkload.service.WorkloadLoadService;
import com.yearEndWorkload.mapper.WorkloadLoadMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @author admin
* @description 针对表【workload_load(工作量导入表)】的数据库操作Service实现
* @createDate 2024-03-06 22:45:24
*/
@Service
public class WorkloadLoadServiceImpl extends ServiceImpl<WorkloadLoadMapper, WorkloadLoad>
    implements WorkloadLoadService{

    @Resource
    private WorkloadLoadMapper workloadLoadMapper;

    @Override
    public Result<?> addWorkloadLoad(AddWorkloadLoadForm addWorkloadLoadForm) {
        WorkloadLoad workloadLoad = new WorkloadLoad();
        workloadLoad.setYear(addWorkloadLoadForm.getYear());
        workloadLoad.setWorkloadType(addWorkloadLoadForm.getWorkloadType());
        workloadLoad.setWorkloadName(addWorkloadLoadForm.getWorkloadName());
        workloadLoad.setTeacherName(addWorkloadLoadForm.getTeacherName());
        workloadLoad.setAmount(addWorkloadLoadForm.getAmount());
        workloadLoad.setNumber(addWorkloadLoadForm.getNumber());
        workloadLoad.setReporter(addWorkloadLoadForm.getReporter());
        workloadLoadMapper.insert(workloadLoad);
        return Result.success(null);
    }

    @Override
    public Result<?> deleteWorkloadLoad(Integer[] ids) {
        workloadLoadMapper.deleteBatchIds(Arrays.asList(ids));
        return Result.success(null);
    }

    @Override
    public Result<List<WorkloadLoad>> getWorkloadLoadList(Long pageNumber, Long pageSize) {
        Page<WorkloadLoad> page = new Page<>(pageNumber,pageSize);
        List<WorkloadLoad> workloadLoadList = workloadLoadMapper.selectPage(page,null).getRecords();
        return Result.success(workloadLoadList);
    }

    @Override
    public Result<?> updateWorkloadLoad(UpdateWorkloadLoadForm updateWorkloadLoadForm) {
        LambdaUpdateWrapper<WorkloadLoad> updateWrapper = new LambdaUpdateWrapper<>();
        workloadLoadMapper.update(null,updateWrapper.eq(WorkloadLoad::getId,updateWorkloadLoadForm.getId())
                .set(WorkloadLoad::getYear,updateWorkloadLoadForm.getYear())
                .set(WorkloadLoad::getWorkloadType,updateWorkloadLoadForm.getWorkloadType())
                .set(WorkloadLoad::getWorkloadName,updateWorkloadLoadForm.getWorkloadName())
                .set(WorkloadLoad::getTeacherName,updateWorkloadLoadForm.getTeacherName())
                .set(WorkloadLoad::getNumber,updateWorkloadLoadForm.getNumber())
                .set(WorkloadLoad::getAmount,updateWorkloadLoadForm.getAmount())
                .set(WorkloadLoad::getReporter,updateWorkloadLoadForm.getReporter()));
        return Result.success(null);
    }

    @Override
    public Result<?> getTeacherWorkload(TeacherWorkloadQuery teacherWorkloadQuery) {
        String teacherId = teacherWorkloadQuery.getTeacherId();
        String yearEndExcelName = teacherWorkloadQuery.getYearEndExcelName();
        List<String> yearEndTableName = YearEndExcelName.getYearEndTableName(yearEndExcelName);
        if(yearEndTableName==null){
            return Result.fail("请选择年终工作量表格！进行查找");
        }
        String teacherName = workloadLoadMapper.getTeacherNameById(teacherId);
        if(teacherName !=null){
            //构建分页查询器
            Page<List<Map<String,Object>>> page = new Page<>(teacherWorkloadQuery.getCurrentPage(), teacherWorkloadQuery.getPageSize());

            QueryCall queryCall = new QueryCall();
            queryCall.setTableName(yearEndTableName.get(0));
            queryCall.setTeacherName(teacherName);
            List<Map<String, Object>> res;
            if(yearEndTableName.size()>1){
                queryCall.setChildName(yearEndTableName.get(1));
                res = workloadLoadMapper.getTeacherYearEndWorkloadWithChild(page,queryCall);
            }
            else {
                res = workloadLoadMapper.getTeacherYearEndWorkloadNotChild(page, queryCall);
            }
            return Result.success(res);
        }else {
            return Result.fail("无该教师");
        }

    }



}




